<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .hidden {
            width: 300px;
            height: 150px;
            margin-top: 50px;
            padding-top: 50px;
            text-align: center;
            background: linear-gradient(to right, rgb(210, 91, 111), rgb(11, 131, 147));
            display: none;
        }
    </style>
</head>

    <body>
        <!--     //页面上输入用户的姓名、性别、年龄 确认后 列表的方式显示添加的数据
            //提示：取出input对应的dom对象后可以通过values属性获取到input中的内容 -->
        <div>
            <input type="text" id="username" />
            <input type="radio" id="sexOne" name="sex" value="男" />男
            <input type="radio" id="sexTwo" checked name="sex" value="女" />女
            <input type="text" id="age" />
            <button id="add">添加</button>
        </div>
        <br />
        <table cellspacing="0" border="1" width="600px" text-align="center">
            <tr align="center">
                <td>姓名</td>
                <td>性别</td>
                <td>年龄</td>
                <td colspan="2">操作</td>
            </tr>
        </table>
        <div class="hidden">
            <input type="text" id="username_change" /><br>
            <input type="radio" id="sexOnechange" name="sexchange" value="男" />男
            <input type="radio" id="sexTwochange" checked name="sexchange" value="女" />女<br>
            <input type="text" id="agechange" /><br>
            <br>
            <button id="addchange">确定</button>
        </div>
        <script>
            document.querySelector("#add").onclick = function () {
                var name = document.querySelector("#username").value;
                var gender = document.querySelector("input[name='sex']:checked").value;
                var age = document.querySelector("#age").value;
                var trDom = document.createElement("tr");
                trDom.innerHTML = `<tr>
                    <td>${name}</td>
                    <td>${gender}</td>
                    <td>${age}</td>
                    <td><button onclick='changeT(this)'>修改</button></td>
                    <td><button onclick="fn(this)">删除</button></td>
                    </tr>`;
                document.querySelector('tbody').appendChild(trDom);
            }  // 未来事件不能采用动态绑定，但是可以使用行内绑定
            function changeT(obj){
                document.querySelector('.hidden').style.display='block';
                document.querySelector('#addchange').onclick=function(){
                document.querySelector('.hidden').style.display='none';
                var name=document.querySelector("#username_change").value;
                var gender = document.querySelector("input[name='sexchange']:checked").value;
                var age=document.querySelector("#agechange").value;
                obj.parentElement.previousElementSibling.innerHTML=age;
                obj.parentElement.previousElementSibling.previousElementSibling.innerHTML=gender;
                obj.parentElement.previousElementSibling.previousElementSibling.previousElementSibling.innerHTML=name;
            }
            }
            
            function fn(obj) {
                //parentElement表示获取到上一级的dom对象
                var trDom = obj.parentElement.parentElement;
                document.querySelector('tbody').removeChild(trDom);
            }       
        </script>

    
</body>
</html>